package com.manxi.mapper;

import com.manxi.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

@Mapper
public interface UserMapper {

    /**
     * 根据openid查询用户
     * @param openid
     * @return
     */
    @Select("select * from user where openid = #{openid}")
    User getByOpenid(String openid);

    /**
     * 注册用户
     * @param user
     */
    @Insert("insert into user (openid,name,phone,sex,id_number,avatar,create_time) values (#{openid},#{name},#{phone},#{sex},#{idNumber},#{avatar},#{createTime})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void insert(User user);

    /**
     * 根据用户id查询用户
     * @param userId
     * @return
     */
    @Select("select * from user where id = #{userId}")
    User getById(Long userId);

    /**
     * 统计用户数量
     * @param map
     */
    @Select("<script> " +
            "select count(id) from user " +
            "where 1 = 1 "+
            "<if test='begin != null'>AND create_time &gt;= #{begin} </if> " +
            "<if test='end != null'>AND create_time &lt;= #{end} </if> " +
            "</script>")
    Integer countByMap(Map map);

}
